import { defHttp } from '/@/common/util/axios';

enum Api {
  list = '/base/baseRel/findPage', //数据列表查询
  batchUpdate = '/base/baseRel/batchUpdate', //批量修改绩效部门
  column = '/base/baseRel/getRelType', //查询动态表头
  refresh = '/base/baseRel/refresh', //刷新
  del = '/base/baseRel/delete', //删除
  personType = '/base/stType/getSuitPersontList', //人员类型
  getDictByCode = '/system/dict-data/list',
  update = '/base/baseRel/update', //单条数据修改
  same = '/base/quartz/saveKmData', //同步
  samePeople = '/base/quartz/saveBaseData', //同步人资
  selectPersonTreeList = '/base/hubKm/getDeptAndPostTree',
}

/**
 * author:lhw
 * description: 分页查询
 * @params: @params 查询参数
 * date: 2023/2/13
 * lastModify: 2023/2/13
 */
export const tableList = (params) => defHttp.get({ url: Api.list, params });

/**
 * author:lhw
 * description: 查询动态表头
 * @params: @
 * date: 2023/2/15
 * lastModify: 2023/2/15
 */
export const getColumn = () => defHttp.post({ url: Api.column });

/**
 * author:lhw
 * description:批量刷新
 * @params: @
 * date: 2023/2/15
 * lastModify: 2023/2/15
 */
export const refresh = (params) => defHttp.post({ url: Api.refresh, params, timeout: 5 * 60 * 1000 });

/**
 * author:lhw
 * description: 批量修改绩效部门
 * @params: @
 * date: 2023/2/14
 * lastModify: 2023/2/14
 */
export const batchUpdate = (params) => defHttp.post({ url: Api.batchUpdate, params });

/**
 * author:lhw
 * description: 删除
 * @params: @
 * date: 2023/2/15
 * lastModify: 2023/2/15
 */
export const deleteRelation = (params) => defHttp.post({ url: Api.del, params }, { joinParamsToUrl: true });

export const getPersonType = () => {
  const params = {
    pageNo: 1,
    pageSize: 100,
  };
  return defHttp.get({ url: Api.personType, params });
};

export const getDictByCode = (dictType) => {
  const params = Object.assign({ pageNo: 1, pageSize: 100 }, dictType);
  return defHttp.get({ url: Api.getDictByCode, params });
};

export const singleUpdate = (params) => defHttp.post({ url: Api.update, params });

export const getSame = (params) => defHttp.get({ url: Api.same, params });
export const getSamePeople = (params) => defHttp.get({ url: Api.samePeople, params });
export const selectPersonTreeList = (params?) => {
  const titleMap = (arr = []) => {
    arr.forEach((i) => {
      // i.title = i.orgName;
      i.value = i.id + '';
      // i.id = i.deptId;
      i.key = i.id + '';
      i.disabled = !i.isLeaf;
      i.psnNum = i.desc ? i.desc.psnNum : i.id + '';
      if (i.desc?.psnNum) {
        i.psnNum = i.desc.psnNum;
        // i.key = i.desc.psnNum + '';
      }
      if (i.children?.length) {
        titleMap(i.children);
      }
    });
  };
  return new Promise((resolve, reject) => {
    defHttp.get({ url: Api.selectPersonTreeList, params }).then((res) => {
      titleMap(res);
      console.log('res-------------岗位人员树', res);
      resolve(res);
    });
  });
  // return defHttp.get({ url: Api.selectSuperDepartTreeList, params });
};
